package cn.kgc.crowdfundingadminspringboot01.controller;

import cn.kgc.crowdfundingadminspringboot01.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadminspringboot01.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot01.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot01.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-19 22:12
 */
@Controller
public class AdminController {

    @Resource
    AdminService adminService;

    @RequestMapping("/admin/to/login/page.html")
    public String toLoginPage() {
        return "admin-login";
    }

    @RequestMapping("/admin/do/login.html")
    public String doLogin(
            @RequestParam("loginAcct") String loginAcct,
            @RequestParam("userPswd") String userPswd,
            Model model,
            HttpSession session) {
        Admin admin = adminService.login(loginAcct, userPswd);
        if (admin == null) {
            //CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
        //CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, admin
        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        return "redirect:/admin/to/main/page.html";
    }

    //跳转后台主页
    @RequestMapping("/admin/to/main/page.html")
    public String toLogin() {
        return "admin-main";
    }

    @RequestMapping("/admin/query/for/search")
    public String queryForSearch(

            // 如果页面上没有提供对应的请求参数，那么可以使用defaultValue指定默认值
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            Model model) {

        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        System.out.println(pageInfo);
        return "admin-page";
    }

    //异常处理
    @ExceptionHandler(value = Exception.class)
    public String catchException(Exception exception, Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception.getMessage());
        return "system-error";
    }

    //退出
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }

    //删除
    @ResponseBody
    @RequestMapping("/admin/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
        try {
            adminService.batchRemove(adminIdList);
            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            return ResultEntity.failed(null, e.getMessage());
        }

    }

    //跳转添加页面
    @RequestMapping("admin/to/add/page.html")
    public String toadd() {
        return "admin-add.html";
    }

    /**
     * 添加用户
     *
     * @param admin
     * @return
     */
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin) {

        adminService.saveAdmin(admin);

        return "redirect:/admin/query/for/search";
    }

    /**
     * 获取用户信息，跳转修改页面
     */
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        Admin admin = adminService.getAdminById(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);
        return "admin-edit";
    }

    /**
     * 修改
     */
    @RequestMapping("/admin/update")
    public String updateAdmin(Admin admin, Integer pageNum) {
        try {
            adminService.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/query/for/search?pageNum=" + pageNum;
    }
}
